package ApplicationLogic.modelValidation;


import java.util.Set;

import javax.validation.*;

import businessLogic.model.AbstractModel;

public class ModelValidator implements IModelValidator 
{

	/* (non-Javadoc)
	 * @see helpers.IModelValidator#isValidModel(modelo.AbstractModel)
	 */
	@Override
	public boolean isValidModel(AbstractModel aModel)
	{
		
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		Validator validator = factory.getValidator();
		Set<ConstraintViolation<AbstractModel>> constraintViolations = validator.validate(aModel);

		if (constraintViolations.size() <= 0) return true;
		else return false;
		
	}

	public String getNotValidModelMessage(AbstractModel aModel)
	{
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		Validator validator = factory.getValidator();
		Set<ConstraintViolation<AbstractModel>> constraintViolations = validator.validate(aModel);
		
		return constraintViolations.iterator().next().getMessage();
	}

}
